
import 'dart:ffi';

import '../common/utility/class_reflection_manager.dart';

class BaseResponseEntity<T> {
  BaseResponseEntity({
    required this.message,
    required this.code,
    required this.data,
  });
  late final String message;
  late final int code;
  late final dynamic? data;

  BaseResponseEntity.fromJson(Map<String, dynamic> json){
    if (json['data'] != null&&json['data']!='null') {
      if(json['data'] is List){
        BaseDataEntity? baseData = ClassReflectionManager.getClassInstance(T.toString());
        if(baseData == null) {
          data = json['data'];
          message = json['message'];
          code = json['code'];
          return;
        }
        List list = [];
        json['data'].forEach((e) {
          list.add(baseData?.reFromJson(e)!) ;
        });
        data = list;
      }else{
        BaseDataEntity? baseData = ClassReflectionManager.getClassInstance(T.toString());
        if(baseData == null) {
          data = json['data'];
        }else{
          Map<String, dynamic> jsonData;
          if(json['data'] is Map<String, dynamic>){
            jsonData = json['data'];
          }else{
            jsonData = {};
          }
          data = baseData?.reFromJson(jsonData) as T?;
        }
      }

    }
    message = json['message'];
    code = json['code'];
  }


  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['message'] = message;
    _data['code'] = code;
    _data['data'] = data;
    return _data;
  }
}


abstract class BaseDataEntity{
  BaseDataEntity? reFromJson(Map<String, dynamic> json);

}